import { getListData, processStart, getForm, startWithExternalForm, isExternalForm } from "services/FlowExecutionApi";
// import { detailFormat } from "../pages/flowmanage/formbuilder/nomalFn";

const initData = {
  flowListData: [],
  totalCount: 0,
  taskformFields: [],
  formUrl: '',
  displayStyle: '',
  showAsUrl: '',
};

export default {
  namespace: "FlowExecution",
  state: {
    ...initData,
  },

  effects: {
    *getFlowListData({ payload }, { call, put }) {
      const {
        datas = {},
        totalCount = 0,
        errMsg,
        errortext,
        errCode,
      } = yield call(getListData, payload);
      yield put({ type: "get/flowListData", payload: datas || {}, totalCount });
      return { errCode, errMsg: errMsg || errortext };
    },
    *processStart({ payload }, { call }) {
      const { errCode, errMsg, errortext } = yield call(processStart, payload);
      return { errCode, errMsg: errMsg || errortext };
    },
    *getForm({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas } = yield call(
        getForm,
        payload,
      );
      const { fields = {}, name = "" } = datas || {};
      // const newFileds = detailFormat(fields || []);
      yield put({ type: "get/getForm", payload: fields, formName: name });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *startWithExternalForm({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas } = yield call(
        startWithExternalForm,
        payload,
      );
      const { formUrl = "", displayStyle = "", showAsUrl =""} = datas || {};
      yield put({ type: "get/startWithExternalForm", payload: formUrl, displayStyle, showAsUrl });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *isExternalForm({ payload }, { call, put }) {
      const resData = yield call(isExternalForm, payload);
      const { errCode, errMsg, datas = {}, errortext } = resData || {};
      yield put({ type: "get/isExternalForm", payload: datas });
      return { errCode, errMsg: errortext || errMsg, datas };
    },
  },
  reducers: {
    "get/flowListData": (state, { payload, totalCount }) => ({
      ...state,
      flowListData: payload,
      totalCount,
    }),
    "get/getForm": (state, { payload, formName }) => ({
      ...state,
      taskformFields: payload,
      formName,
    }),
    "get/startWithExternalForm": (state, { payload, displayStyle, showAsUrl}) => ({
      ...state,
      formUrl: payload,
      displayStyle,
      showAsUrl,
    }),
    "get/isExternalForm": (state, { payload }) => ({
      ...state,
      isExternalForm: payload,
    }),
    clearData: () => ({ ...initData }),
  },
};
